package fantasyday.wowzolo.web.common;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;

import java.util.List;

@SuppressWarnings("serial")
public class DropDownChoiceDefaultValue extends DropDownChoice {
	private int _defaultModelIndex;

	public DropDownChoiceDefaultValue(final String id, final List<String> choices, int defaultModelIndex) {
		super(id, choices);
		_defaultModelIndex = defaultModelIndex;
	}

	public DropDownChoiceDefaultValue(final String id, final List<String> data, final IChoiceRenderer renderer, int defaultModelIndex) {
		super(id, data, renderer);
		_defaultModelIndex = defaultModelIndex;
	}

	@Override
	public String getModelValue() {
		Object object = getModelObject();
		if (object != null) {
			int index = getChoices().indexOf(object);
			return getChoiceRenderer().getIdValue(object, index);
		} else {
			object = getChoices().get(_defaultModelIndex);
			return getChoiceRenderer().getIdValue(object, _defaultModelIndex);
		}
	}
}
